/*  
 *  Nanjing JIYU Technology Co.,Ltd All rights reserved.
 *  Copyright (C) 2022 
 *  ==============================================================
 * 
 *  controlled.c 
 *    受控运行功能的实现
 *
 *  --------------------------------------------------------------
 *  
 */

#include "controlled.h"

#include "track.h"

#include "device.h"


/* 上位机控制下左转 */
static void ctrl_turn_left(void)
{
  track_turn_left(speed_medium);
}


/* 上位机控制下右转 */
static void ctrl_turn_right(void)
{
  track_turn_right(speed_medium);
}


/* 上位机控制下前进 */
static void ctrl_drive(void)
{
  /* 往前开一点，防止当前十字路口误判 */
  track_drive(speed_low);
  delay_ms(300);

  /* 循迹控制循环 */
  while (1)
  {
    delay_ms(20);
    track_drive(speed_low);

    /* 遇到十标号，停车，跳出控制循环 */
    if (gray_is_cross())
    {
      track_stop();
      break;
    }
  }
}


static void ctrl_reverse(void)
{
  track_reverse(speed_low);
  delay_ms(300);

  /* 循迹控制循环 */
  while (1)
  {
    delay_ms(20);
    track_reverse(speed_low);

    /* 遇到十标号，停车，跳出控制循环 */
    if (gray_is_cross())
    {
      track_stop();
      break;
    }
  }
}


void ctrl_run(unsigned char cmd)
{
  switch (cmd)
  {
    case 'w' :
      ctrl_drive();
      break;
    case 's' :
      ctrl_reverse();
      break;
    case 'a' :
      ctrl_turn_left();
      break;
    case 'd' :
      ctrl_turn_right();
      break;
    case 'b' :
      ctrl_turn_left();
      break;
    case 'c' :
      ctrl_turn_right();
      break;
    default   :
      break;
  }
}








